package com.gs.SynAntiWandering.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gs.SynAntiWandering.R;

public class ComboPicDialog extends BaseDialog {

    public interface OnClickListener {
        void camera();

        void gallery();

        void cancel();
    }

    private String title = "";
    private TextView item_title;
    private TextView item_cancel;
    private TextView item_camera;
    private TextView item_gallery;
    private OnClickListener onClickListener;
    public static final int PIC_CAREMA = 000001;// 拍照
    public static final int PIC_GALLERY = 0000002;// 从相册中选择
    public static final int PIC_CUT = 0000003;// 结果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public ComboPicDialog(@NonNull Context context) {
        super(context, R.style.custom_dialog);
    }

    public ComboPicDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    @Override
    public void refreshView() {
        if (!TextUtils.isEmpty(title)) {
            item_title.setText(title);
        }
    }

    @Override
    public void initView() {
        item_title = findViewById(R.id.item_title);
        item_cancel = findViewById(R.id.item_cancel);
        item_gallery = findViewById(R.id.item_gallery);
        item_camera = findViewById(R.id.item_camera);
        item_camera.setOnClickListener(new CustomDialogClickListener());
        item_gallery.setOnClickListener(new CustomDialogClickListener());
        item_cancel.setOnClickListener(new CustomDialogClickListener());
    }

    public ComboPicDialog setClicklistener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        return this;
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_combo_pic;
    }

    @Override
    public void show() {
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        this.setCanceledOnTouchOutside(true);
        super.show();
        refreshView();
        //获得window窗口的属性
        WindowManager.LayoutParams params = window.getAttributes();
        //设置窗口宽度为充满全屏
        params.width = WindowManager.LayoutParams.MATCH_PARENT;//如果不设置,可能部分机型出现左右有空隙,也就是产生margin的感觉
        //设置窗口高度为包裹内容
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        // params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;//显示dialog的时候,就显示软键盘
        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;//就是这个属性导致window后所有的东西都成暗淡
        params.dimAmount = 0.5f;//设置对话框的透明程度背景(非布局的透明度)
        //将设置好的属性set回去
        window.setAttributes(params);
    }

    private class CustomDialogClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if (id == R.id.item_cancel) {
                onClickListener.cancel();
            } else if (id == R.id.item_camera) {
                onClickListener.camera();
            } else if (id == R.id.item_gallery) {
                onClickListener.gallery();
            }
        }
    }
}
